***********************************************
******* YIELD CURVES - SPAIN AND FRANCE *******
***********************************************
/*
In this do file:

	FIRST: We import the data of the Dynamic Nelson Siegel 
	Svensson model for Spain and France.

	SECOND: We aggregate the data at monthly frequency (for Spain
	and France).

	THIRD: We aggregate the data at quarterly frequency (for Spain
	and France). 

*/

*----------------
* PATHS
*----------------
global replication `c(pwd)'
global data "$replication\Data"
global inter "$replication\Dta - Intermediate"
global final "$replication\Dta - Final"
global graphs "$replication\Graphs"

***********************************************
* SPANISH DATA
***********************************************
*================
* HOUSEKEEPING
*================
clear all
import excel "$data\Christensen_DNSSModel.xlsx", sheet("Sheet1") cellrange(A1:E5217) firstrow clear

* Changing Variable Names
label var beta0 "Level Factor"
label var beta1 "Slope Factor"
label var beta2 "Curvature Factor 1"
label var beta3 "Curvature Factor 2"

* Formatting date variables
format Date %td
tsset Date
rename Date dates

* We create the lambda parameters
scalar lambda1=0.9620208
scalar lambda2=0.4572537

* Constructing Yields - Based on Time Series
forvalues i=0(3)600{
	gen r_`i'=beta0+(beta1+beta2)*(1-exp(-lambda1*`i'))/(lambda1*`i')-beta2*exp(-lambda1*`i')+beta3*((1-exp(-lambda2*`i'))/(lambda2*`i')-exp(-lambda2*`i'))
}

save "$inter\ESP_DNSS_RawData.dta", replace

*=====================
* MONTHLY AGGREGATION
*=====================
preserve
gen my=mofd(dates)
label var my "Issuance Month Year"

format my %tm
collapse beta* r_*, by(my)
tsset my

* Changing Variable Names
label var beta0 "Level Factor"
label var beta1 "Slope Factor"
label var beta2 "Curvature Factor 1"
label var beta3 "Curvature Factor 2"

save "$inter\ESP_DNSS_MonthlyData.dta", replace

restore

*=======================
* QUARTERLY AGGREGATION
*=======================
g qy=qofd(dates)
label var qy "Issuance Month Year"

format qy %tq
collapse beta* r_*, by(qy)
tsset qy

* Changing Variable Names
label var beta0 "Level Factor"
label var beta1 "Slope Factor"
label var beta2 "Curvature Factor 1"
label var beta3 "Curvature Factor 2"

save "$inter\ESP_DNSS_QuarterlyData.dta", replace

*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

***********************************************
* FRENCH DATA
***********************************************
*================
* HOUSEKEEPING
*================
clear all
import excel "$data\Christensen_DNSS.France.xlsx", sheet("Sheet1") cellrange(A1:E5217) firstrow clear

* Changing Variable Names
label var beta0 "Level Factor"
label var beta1 "Slope Factor"
label var beta2 "Curvature Factor 1"
label var beta3 "Curvature Factor 2"

* Formatting date variables
format Date %td
tsset Date
rename Date dates

* We create the lambda parameters
scalar lambda1=0.9620208
scalar lambda2=0.4572537

* Constructing Yields - Based on Time Series
forvalues i=0(3)600{
	gen r_`i'=beta0+(beta1+beta2)*(1-exp(-lambda1*`i'))/(lambda1*`i')-beta2*exp(-lambda1*`i')+beta3*((1-exp(-lambda2*`i'))/(lambda2*`i')-exp(-lambda2*`i'))
}

save "$inter\FR_DNSS_RawData.dta", replace

*=====================
* MONTHLY AGGREGATION
*=====================
preserve
g my=mofd(dates)
label var my "Issuance Month Year"

format my %tm
collapse beta* r_*, by(my)
tsset my

* Changing Variable Names
label var beta0 "Level Factor"
label var beta1 "Slope Factor"
label var beta2 "Curvature Factor 1"
label var beta3 "Curvature Factor 2"

save "$inter\FR_DNSS_MonthlyData.dta", replace

restore

*=======================
* QUARTERLY AGGREGATION
*=======================
g qy=qofd(dates)
label var qy "Issuance Month Year"

format qy %tq
collapse beta* r_*, by(qy)
tsset qy

* Changing Variable Names
label var beta0 "Level Factor"
label var beta1 "Slope Factor"
label var beta2 "Curvature Factor 1"
label var beta3 "Curvature Factor 2"

save "$inter\FR_DNSS_QuarterlyData.dta", replace
